package panda.android.notebook.popupWin;

import panda.android.notebook.R;
import panda.android.notebook.Utils.Utils;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.PopupWindow;

public class SignPopWin {

	public interface SignClick {
		public void signclick(String tag);
	}

	private SignClick itemClick;
	private PopupWindow popupWindow;

	private String mStrings[] = { "。", "，", "、", "；", "：", "？", "！", "……", "—",
			"~", "’", "‘", "”", "“", "《", "》", "|", "【", "】", "｛", "｝", "=",
			"+", "-", "_", "（", "）" };
	private Activity mActivity;

	private boolean isShow = false;

	public void setSignClick(SignClick itemClick) {
		this.itemClick = itemClick;
	}

	public SignPopWin(Activity activity) {
		// TODO Auto-generated constructor stub
		mActivity = activity;
	}

	public void show(View v) {
		// TODO Auto-generated method stub
		if (isShow) {
			dismiss();
			return;
		}

		GridView gridView = new GridView(mActivity);
		gridView.setBackgroundResource(R.drawable.button_white);
		gridView.setCacheColorHint(Color.TRANSPARENT);
		gridView.setSelector(R.drawable.transparent);
		gridView.setAdapter(new gridAdapter());
		gridView.setHorizontalSpacing(2);
		gridView.setVerticalSpacing(2);
		gridView.setNumColumns(8);

		Utils utils = new Utils();
		popupWindow = new PopupWindow(gridView, utils.width(300), utils.height(150)) {
			@Override
			public void dismiss() {
				// TODO Auto-generated method stub
				super.dismiss();
				isShow = false;
			}
		};
		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.showAsDropDown(v);
		isShow = true;
	}

	public void dismiss() {
		popupWindow.dismiss();
		isShow = false;
	}

	class gridAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mStrings.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mStrings[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				Button button = new Button(mActivity);
				button.setText(mStrings[position]);
				Utils utils = new Utils();
				button.setTextSize(utils.fontsize(10));
				button.setTag(mStrings[position]);
				button.setBackgroundResource(R.drawable.button);
				button.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						itemClick.signclick(v.getTag().toString());
					}
				});
				return button;
			}
			return convertView;
		}

	}
}
